package com.yxy.controller.admin;

import com.yxy.common.PageResult;
import com.yxy.common.Result;
import com.yxy.constant.RequestMapperConstant;
import com.yxy.pojo.dto.NewsDTO;
import com.yxy.pojo.dto.NewsPageQueryDTO;
import com.yxy.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminNewsController")
@RequestMapping(RequestMapperConstant.ADMIN_REQUEST_MAPPER + "news")
@Slf4j
public class NewsController {

    @Autowired
    private NewsService newsService;

    /**
     * 分页查询资讯列表
     * @param newsPageQueryDTO
     * @return
     */
    @GetMapping("/pageQuery")
    public Result<PageResult> pageQuery(NewsPageQueryDTO newsPageQueryDTO){
        log.info("分页查询资讯列表：{}", newsPageQueryDTO);
        PageResult pageResult = newsService.pageQuery(newsPageQueryDTO);
        return Result.success("", pageResult);
    }

    /**
     * 添加资讯
     * @param newsDTO
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody NewsDTO newsDTO){
        log.info("添加资讯信息：{}", newsDTO);
        newsService.add(newsDTO);
        return Result.success("添加成功");
    }

    /**
     * 批量删除资讯
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds")
    public Result batchDelete(@RequestParam int[] ids){
        log.info("批量删除资讯信息：{}", ids);
        newsService.batchDelete(ids);
        return Result.success("删除成功");
    }

    /**
     * 更新资讯
     * @param newsDTO
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody NewsDTO newsDTO){
        log.info("更新资讯信息：{}", newsDTO);
        newsService.update(newsDTO);
        return Result.success("修改成功");
    }
}
